package org.dromara.system.controller.system;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.vo.SysStoreAreaRequestVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.system.domain.vo.SysStoreAreaVo;
import org.dromara.system.domain.bo.SysStoreAreaBo;
import org.dromara.system.service.ISysStoreAreaService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 系统管理-店内分区配置
 * 前端访问路由地址为:/system/storeArea
 *
 * @author LionLi
 * @date 2024-03-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/storeArea")
public class SysStoreAreaController extends BaseController {

    private final ISysStoreAreaService sysStoreAreaService;

    /**
     * 查询系统管理-店内分区配置列表
     */
    @SaCheckPermission("system:storeArea:list")
    @GetMapping("/list")
    public TableDataInfo<SysStoreAreaVo> list(SysStoreAreaBo bo, PageQuery pageQuery) {
        return sysStoreAreaService.queryPageList(bo, pageQuery);
    }


    /**
     * 新增系统管理-店内分区配置
     */
    @SaCheckPermission("system:storeArea:add")
    @Log(title = "系统管理-店内分区配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysStoreAreaRequestVo vo) {
        List<SysStoreAreaBo> storeAreaBos = vo.getStoreAreas();
        for (SysStoreAreaBo bo : storeAreaBos) {
            bo.setStoreId(vo.getStoreId());
            //通一门店下面店内分区编码唯一
            if (!sysStoreAreaService.checkStoreAreaCode(bo)) {
                return R.fail("同一门店下面,添加参数'" + bo.getStoreAreaCode()+ "'失败，店内分区编码已存在");
            }

        }
        return toAjax(sysStoreAreaService.insertList(storeAreaBos));
    }

    /**
     * 修改系统管理-店内分区配置
     */
    @SaCheckPermission("system:storeArea:edit")
    @Log(title = "系统管理-店内分区配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysStoreAreaBo bo) {
        if (!sysStoreAreaService.checkStoreAreaCode(bo)) {
            return R.fail("同一门店下面,修改参数'" + bo.getStoreAreaCode()+ "'失败，店内分区已存在");
        }
        return toAjax(sysStoreAreaService.updateByBo(bo));
    }

    /**
     * 删除系统管理-店内分区配置
     *
     * @param storeAreaId 主键串
     */
    @SaCheckPermission("system:storeArea:remove")
    @Log(title = "系统管理-店内分区配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{storeAreaId}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String storeAreaId) {
        long storeAreaIdLong = Long.parseLong(storeAreaId);
        return toAjax(sysStoreAreaService.deleteWithValidById(storeAreaIdLong));
    }
}
